#ifndef _CRT_SECURE_NO_WARNINGS
	#define _CRT_SECURE_NO_WARNINGS // Deal with warnings in glui.h
#endif

#include <GL\glui.h>
#include "Objects.h"
#include "walls.h"
#include "Functions.h"
#include "Textures.h"
#include <math.h>

Vector x_axis(1,0,0);

float compressor_x = (3/4)*ROOMX/2;
float wheel_length = 0.5;
float wheel_lambda = 0.4;
float robot_size  = compressor_x;

GLdouble clipper[4] = {0.0, 1.0, 0.0, 0.0};

// declaracoes para as paredes
float metal_shininess[] = {89.6}; 
float metal_specular[] = {0.773911, 0.773911, 0.773911, 1.0};	/* specular reflection. */
float metal_diffuse[] =  {0.2775, 0.2775, 0.2775, 1.0};	/* diffuse reflection. */
float metal_ambient[] =  {0.6, 0.6, 0.6, 1.0};	/* ambient reflection. */

void draw_print_machine() {


	int tex[6] = {WOODP_TEX_ID, WOODP_TEX_ID, WOODP_TEX_ID, WOODP_TEX_ID, WOODP_TEX_ID, WOODP_TEX_ID};

	glPushMatrix();
		glTranslated(PRINTER_X, PRINTER_Y, PRINTER_Z);
		glScaled(PRINTER_DIM_X, PRINTER_DIM_Y, PRINTER_DIM_Z);
		drawCube(tex);
	glPopMatrix();


	glPushMatrix();
		glTranslatef(TEAPOT_X, TEAPOT_Y + TEAPOT_DIM/2 + TEAPOT_LAMBDA + 0.5, TEAPOT_Z);
		glColor3d(0.8, 0.87, 0.51);
		glutSolidTeapot(TEAPOT_DIM);
	glPopMatrix();


}

void draw_print_cylinder(GLUquadric* glQ) {
	glColor3f(1.0,1.0,1.0);			

	glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, LETTERS_TEX2_ID);
	gluQuadricTexture(glQ, GL_TRUE);

	glPushMatrix();

		glTranslatef(CYLINDER_X, CYLINDER_Y, CYLINDER_Z);

		glPushMatrix();
			glRotated(90, 0.0, 1.0, 0.0);
			gluCylinder(glQ, AXIS_RADIUS, AXIS_RADIUS, CYLINDER_LENGTH, 20, 5);
		glPopMatrix();

		glDisable(GL_TEXTURE_2D);

		glPushMatrix();
			glTranslated(CYLINDER_LENGTH, 0, 0);
			glRotated(90, 0.0, 1.0, 0.0);
			gluDisk(glQ, 0, AXIS_RADIUS,
							 ROBOT_WHEEL_SLICES, 1);  
		glPopMatrix();

		glPushMatrix();
			glRotated(-90, 0.0, 1.0, 0.0);
			gluDisk(glQ, 0, AXIS_RADIUS,
							 ROBOT_WHEEL_SLICES, 1);  
		glPopMatrix();

	glPopMatrix();

	
}

void draw_line_compressor(void) {
	int tex[6] = {WOOD_TEX_ID, WOOD_TEX_ID, WOOD_TEX_ID, WOOD_TEX_ID, COMPRESSOR_TEX_ID, WOOD_TEX_ID};
	/*draw_prism(compressor_x, ROOMY/4, (3/4)*ROOMZ,                                                                                                                                                                                                                                                                                                                                            (ROOMX/2, ROOMX/2, ROOMX/2, 
		-ROOMX/2, 0, -ROOMZ + ROOMZ/4 + ROOMX/2);*/

	glPushMatrix();
		glTranslated(COMPRESSOR_X, COMPRESSOR_Y, COMPRESSOR_Z);
		glScaled(COMPRESSOR_DIM_X, COMPRESSOR_DIM_Y, COMPRESSOR_DIM_Z);
		drawCube(tex);
	glPopMatrix();

}

void draw_robot(GLUquadric* glQ) {
	// Robot-box

	glPushMatrix();
		glTranslated(ROBOT_X, ROBOT_Y, ROBOT_Z);
	
		// XZ
		glPushMatrix();
			glRotated(-90, 1, 0, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_SQUARE, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

	
		// -YZ Inner Face
		glPushMatrix();
			glTranslated(- ROBOT_DIM_X/2 - ROBOT_CORRECTION, ROBOT_DIM_Y/2, 0);
			glRotated(90, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

	
		// -YZ Back Face
		glPushMatrix();
			glTranslated(- ROBOT_DIM_X/2 - ROBOT_CORRECTION, ROBOT_DIM_Y/2, 0);
			glRotated(-90, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

		
		// +YZ Inner Face
		glPushMatrix();
			glTranslated( ROBOT_DIM_X/2 + ROBOT_CORRECTION, ROBOT_DIM_Y/2, 0);
			glRotated(90, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();
	
	
		// +YZ Back Face
		glPushMatrix();
			glTranslated( ROBOT_DIM_X/2 + ROBOT_CORRECTION, ROBOT_DIM_Y/2, 0);
			glRotated(-90, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

	
		// -YX Inner Face
		glPushMatrix();
			glTranslated(0, ROBOT_DIM_Y/2, -ROBOT_DIM_Z/2 - ROBOT_CORRECTION);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

		// -YX Back Face
		glPushMatrix();
			glTranslated(0 , ROBOT_DIM_Y/2, -ROBOT_DIM_Z/2 - ROBOT_CORRECTION);
			glRotated(180, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

		
		// +YX Inner Face
		glPushMatrix();
			glTranslated(0, ROBOT_DIM_Y/2, +ROBOT_DIM_Z/2 + ROBOT_CORRECTION);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();

		// +YX Back Face
		glPushMatrix();
			glTranslated(0 , ROBOT_DIM_Y/2, +ROBOT_DIM_Z/2 + ROBOT_CORRECTION);
			glRotated(180, 0, 1, 0);
			glScaled(ROBOT_DIM_X, ROBOT_DIM_Y, ROBOT_DIM_Z);
			drawRect(ROBOT_TEX_ID);
		glPopMatrix();
	
		drawRod(glQ);

		glPushMatrix();
			glTranslated(ROBOT_WHEEL_X , ROBOT_WHEEL_Y, ROBOT_WHEEL_Z);
			draw_wheel(glQ);
		glPopMatrix();

		glPushMatrix();
			glTranslated(-ROBOT_WHEEL_X , ROBOT_WHEEL_Y, ROBOT_WHEEL_Z);
			draw_wheel(glQ);
		glPopMatrix();

		glPushMatrix();
			glTranslated(ROBOT_WHEEL_X , ROBOT_WHEEL_Y, -ROBOT_WHEEL_Z - 0.25);
			draw_wheel(glQ);
		glPopMatrix();

		glPushMatrix();
			glTranslated(-ROBOT_WHEEL_X , ROBOT_WHEEL_Y, -ROBOT_WHEEL_Z - 0.25);
			draw_wheel(glQ);
		glPopMatrix();

	glPopMatrix();

	

}

void drawRod(GLUquadric* glQ) {

	glPushMatrix();
		glTranslated(LIGHT_STICK_X, LIGHT_STICK_LENGTH + LIGHT_STICK_Y, LIGHT_STICK_Z);
		
		glPushMatrix();
			glRotated(90, 1, 0, 0); 
			gluCylinder(glQ, LIGHT_STICK_RADIUS, LIGHT_STICK_RADIUS,
							 LIGHT_STICK_LENGTH, LIGHT_STICK_SLICES, 4);  
		glPopMatrix();

		glPushMatrix();
			glTranslated(LIGHT_STICK_RADIUS*2, 0, 0);
			glClipPlane(GL_CLIP_PLANE0, &clipper[0]);
			glEnable(GL_CLIP_PLANE0);
			glutSolidTorus(LIGHT_STICK_RADIUS, LIGHT_STICK_RADIUS*2, LIGHT_STICK_SLICES, 30);
			glDisable(GL_CLIP_PLANE0);
		glPopMatrix();

	glPopMatrix();
	
}

void draw_wheel(GLUquadric* glQ) {

	glPushMatrix();
		gluCylinder(glQ, ROBOT_WHEEL_RADIUS, ROBOT_WHEEL_RADIUS,
						 ROBOT_WHEEL_LENGTH, ROBOT_WHEEL_SLICES, 1);  
	glPopMatrix();

	glPushMatrix();
		glTranslated(0, 0, +ROBOT_WHEEL_LENGTH);
		gluDisk(glQ, 0, ROBOT_WHEEL_RADIUS,
						 ROBOT_WHEEL_SLICES, 1);  
	glPopMatrix();

	glPushMatrix();
		glRotated(180, 0 , 1, 0);
		gluDisk(glQ, 0, ROBOT_WHEEL_RADIUS,
						  ROBOT_WHEEL_SLICES, 1);  
	glPopMatrix();
}

void drawGuillotineRods() {

	int tex[6] = {GUILL_TEX_ID, GUILL_TEX_ID, GUILL_TEX_ID, GUILL_TEX_ID, GUILL_TEX_ID, GUILL_TEX_ID};

	glPushMatrix();
		
		glTranslated(GUILLOTINE_X, GUILLOTINE_Y, GUILLOTINE_Z);

		glPushMatrix();
			glTranslated(-GUILLOTINE_RODS_DISTANCE, GUILLOTINE_ROD_DIM_Y/2, 0);
			glScaled(GUILLOTINE_ROD_DIM_X, GUILLOTINE_ROD_DIM_Y, GUILLOTINE_ROD_DIM_Z);
			drawCube(tex);
		glPopMatrix();

		glPushMatrix();
			glTranslated(+GUILLOTINE_RODS_DISTANCE, GUILLOTINE_ROD_DIM_Y/2, 0);
			glScaled(GUILLOTINE_ROD_DIM_X, GUILLOTINE_ROD_DIM_Y, GUILLOTINE_ROD_DIM_Z);
			drawCube(tex);
		glPopMatrix();

	glPopMatrix();
}


void drawGuillotineCutter() {

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,  metal_ambient);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   metal_diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  metal_specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, metal_shininess);

	glPushMatrix();
		glTranslated(GUILLOTINE_X, GUILLOTINE_CUTTER_Y, GUILLOTINE_Z);
		glScaled(GUILLOTINE_CUTTER_DIM_X, GUILLOTINE_CUTTER_DIM_Y, GUILLOTINE_CUTTER_DIM_Z);
		drawRect(GUILL_TEX_ID);
	glPopMatrix();

	glPushMatrix();
		glTranslated(GUILLOTINE_X, GUILLOTINE_Y + GUILLOTINE_ROD_DIM_Y/2 + GUILLOTINE_CUTTER_DIM_Y*3/4, GUILLOTINE_Z);
		glRotated(180, 0, 1, 0);
		glScaled(GUILLOTINE_CUTTER_DIM_X, GUILLOTINE_CUTTER_DIM_Y, GUILLOTINE_CUTTER_DIM_Z);
		drawRect(GUILL_TEX_ID);
	glPopMatrix();

}
